﻿#region License

// Another free, open solution by folks who get it.
// Copyright © 2011 Business in Unison, Inc.
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#endregion

using System;

using Microsoft.Practices.ServiceLocation;

using Patterns.Inversion;

using TechTalk.SpecFlow;

namespace Patterns.Specifications.Inversion
{
	[Binding]
	public class ContainerSteps : BaseStepDefinition
	{
		public const string RegistryKey = "registry.instance";
		public const string LocatorKey = "locator.instance";

		private static readonly Lazy<IServiceLocator> _lazyLocator = new Lazy<IServiceLocator>(() => CurrentRegistry.CreateLocator());

		public static IServiceRegistry CurrentRegistry
		{
			get { return Get<IServiceRegistry>(RegistryKey); }
			set { Set(value, RegistryKey); }
		}

		public static IServiceLocator CurrentLocator
		{
			get { return _lazyLocator.Value; }
		}

		[Given(@"I have created a (.+)-based registry")]
		public void CreateRegistry(string containerName)
		{
			CurrentRegistry = InversionProvider.CreateRegistry(containerName);
		}

		[When(@"I register a type")]
		public void RegisterType()
		{
			CurrentRegistry.RegisterType<SimpleTestObject>();
		}

		[When(@"I resolve the type I registered")]
		public void ResolveType()
		{
			GeneralSteps.CurrentResult = CurrentLocator.GetInstance<SimpleTestObject>();
		}
	}
}